package com.example.Project.user;

import com.example.Project.Class.User;
import com.example.Project.user.userHttpMethods.userMethods;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;

import java.io.IOException;

public class LoginController {

    @FXML
    private Label ac;

    @FXML
    private TextField account;

    @FXML
    private RadioButton administrators;

    @FXML
    private ToggleGroup identity;

    @FXML
    private Button logon;

    @FXML
    private PasswordField password;

    @FXML
    private Label pw;

    @FXML
    private Button register;

    @FXML
    private RadioButton user;

    @FXML
    void clickregister(MouseEvent event) throws IOException {
        RegisterController enter=new RegisterController();
        enter.start();
    }

    String Identity;
    String Account;
    String Password;

    @FXML
    void logon(MouseEvent event) throws IOException {
        Identity = String.valueOf((RadioButton) identity.getSelectedToggle());
        if(account.getText().isEmpty()||password.getText().isEmpty()||Identity==null){
            Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
            alert.setHeaderText("请将信息填写完整！！！");
            alert.getButtonTypes().add(ButtonType.YES);
            alert.showAndWait();
            return;
        }
        int start = Identity.indexOf("'");
        int end = Identity.lastIndexOf("'");
        if (start != -1 && end != -1 && start < end) {
            Identity = Identity.substring(start + 1, end);
            System.out.println(Identity);
        }
        Account = account.getText();
        Password=password.getText();
        System.out.println(Account+Password);
        boolean flag=userMethods.login(Identity,Account,Password);
        if(flag&&Identity.equals("管理员")){
            HelloController enter=new HelloController();
            enter.start();
            stage=(Stage)pw.getScene().getWindow();
            stage.close();
        }
        else if(flag&&Identity.equals("用户")){
            User user=userMethods.getUserInfo(Integer.parseInt(account.getText()));
            if(user!=null) {
                UserController enter = new UserController();
                enter.get(user);
                enter.start();
            }
            stage=(Stage)pw.getScene().getWindow();
            stage.close();
        }else {
            Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
            alert.setHeaderText("账号或密码错误，请重新填写！！！");
            alert.getButtonTypes().add(ButtonType.YES);
            alert.showAndWait();
            return;
        }
    }
    userMethods userMethods=new userMethods();
    Stage stage=new Stage();
    public void start() throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("login.fxml"));
        Scene scene = new Scene((Parent)fxmlLoader.load(), 546.0, 400.0);
        stage.setTitle("学生信息管理-登录");
        stage.setScene(scene);
        stage.setResizable(false);
        stage.show();
    }

}

